import { createAction } from 'redux-actions';
import { ADD_NETWORK, GET_ALL_NETWORK, UPDATE_NETWOERK_ID, MODIFY_DEVICE, REMOVE_DEVICE, MOVE_DEVICE, DELETE_NETWORK, SWITCH_VPN, CHECK_STATUS, REFRESH_VPN } from '../types/network';
import { fetch } from '../../utils/fetch';
import tip from '../../utils/tip';

/**createAction创建一个action 第一个参数是action类型 第二个参数是一个function 处理并返回需要的payload */
export const addNetwork = createAction(ADD_NETWORK, async (data) => {
    return { data: data }
})

export const getAllNetwork = createAction(GET_ALL_NETWORK, async () => {
    const params = {
        api: 'vppn/sdkapi/v1/client/getAllNetworkOfUser'
    };
    let res = await fetch(params);
    if (res && res.code == 200) {
        return { dataList: res.responseData };
    } else if (res && res.code != 200) {
        tip.alert(res.msg);
    }
}
)

export const deleteNetwork = createAction(DELETE_NETWORK, async (args) => {
    const { data } = args;
    const params = {
        api: 'vppn/sdkapi/v1/client/deleteNetwork',
        data: data
    };
    let res = await fetch(params);
    if (res && res.code == 200) {
        return { teamId: data.networkId }
    }
})

export const updateNetworkById = createAction(UPDATE_NETWOERK_ID, async (data) => {
    const { networkId } = data;
    const params = {
        api: 'vppn/sdkapi/v1/client/getNetworkById',
        data: {
            networkId: networkId
        },
    }
    let res = await fetch(params);
    if (res && res.code == 200) {
        return { data: res.responseData }
    }
})

export const modifyDevice = createAction(MODIFY_DEVICE, async (args) => {
    const { data } = args;
    const params = {
        api: 'vppn/sdkapi/v1/client/modifyDevice',
        data: data
    };
    let res = await fetch(params);
    if (res && res.code == 200) {
        return { data }
    } else if (res && res.code != 200) {
        tip.alert(res.msg);
    }
})

export const removeDevice = createAction(REMOVE_DEVICE, async (args) => {
    const { data } = args;
    const params = {
        api: 'vppn/sdkapi/v1/client/removeDeviceFromNetwork',
        data: data
    };
    let res = await fetch(params);
    if (res && res.code == 200) {
        return { data }
    } else if (res && res.code != 200) {
        tip.alert(res.msg);
    }
})

export const moveDeviceNetwork = createAction(MOVE_DEVICE, async (args) => {
    const { data } = args;
    const params = {
        api: 'vppn/sdkapi/v1/client/moveDeviceNetwork',
        data: data
    };
    let res = await fetch(params);
    if (res && res.code == 200) {
        console.log(res);
        return { data }
    } else if (res && res.code != 200) {
        tip.alert(res.msg);
    }
})

export const swtichVpn = createAction(SWITCH_VPN, async (args) => {
    const { data, value } = args;
    let api = value ? 'vppn/sdkapi/v1/client/startVPNOfRouter' : 'vppn/sdkapi/v1/client/stopVPNOfRouter';
    let hideLoading = true;
    const params = { api, data, hideLoading };
    let res = await fetch(params);
    if (res && res.code == 200) {
        //开启成功
        return { data: { ...data, result: 'success' } };
    }
})

export const refreshVPN = createAction(REFRESH_VPN, async (args) => {
    const { data, status, teamId } = args;
    const api = "vppn/sdkapi/v1/client/getNetworkBySerialNum";
    const params = { data, api };
    let res = await fetch(params);
    if (res && res.code == 200) {
        const { responseData } = res;
        if (responseData.length > 0) {
            let changeStatus = 0;
            for (let item of responseData) {
                if (item.teamId == teamId) {
                    if (status != item.members[0].status) {
                        changeStatus = 1;
                    }
                    return { data: { itemRes: item, changeStatus } };
                };
            }
        }
        return { data: { itemRes: {}, changeStatus } };
    }
})

export const checkStatus = createAction(CHECK_STATUS, async (args) => {
    const { data } = args
    const params = {
        api: 'vppn/api/v1/client/CheckDeviceStatus',
        data
    };
    let res = await fetch(params);
    if (res && res.code == 200) {

    } else if (res && res.code != 200) {
        tip.alert(res.msg);
    }
})




